﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProcurementManagement.Applcation.Command.ProcurementReturnCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using static System.Net.WebRequestMethods;

namespace ProcurementManagement.Controllers
{
    /// <summary>
    /// 采购退货
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ProcurementReturnController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        HttpShow _http;

        public ProcurementReturnController(IMediator mediatorp, HttpShow _http)
        {
            this.mediatorp = mediatorp;
            this._http = _http;
        }


        /// <summary>
        /// 添加采购退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProcurementReturn(AddProcurementReturn command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                command.AddName = name;
                command.AddTime = DateTime.Now;
                result.Data = await mediatorp.Send(new AddProcurementReturnCommand { AddDto= command });
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 显示采购退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ListProcurementReturnDto>>> GetProcurementReturnList([FromQuery] ListProcurementReturnCommand command)
        {
            ApiResult<PageDto<ListProcurementReturnDto>> result = new ApiResult<PageDto<ListProcurementReturnDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProductList(string? PName, string? PNumber, int pindex, int psize, int? ProductTypeId)
        {
            return Ok(await _http.GetProductList(PName, PNumber, pindex, psize, ProductTypeId));
        }

        /// <summary>
        /// 反填采购退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FanProcurementReturnProduct>> FanProcurementReturn([FromQuery]FanProcurementReturnCommand command)
        {
            ApiResult<FanProcurementReturnProduct> result = new ApiResult<FanProcurementReturnProduct>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 修改采购退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdProcurementReturn(AddProcurementReturn command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(new UpdProcurementReturnCommand { UpdDto = command });
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


    }
}
